package cn.webrx;


import cn.webrx.beans.User;
import cn.webrx.vo.UserVo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootApplication
@RestController
public class App {
    @GetMapping("/")
    public Map<String,Object> index(){
        Map<String,Object> map = new HashMap<>();
        map.put("code",200);
        map.put("msg","message");
        List<Map<String,Object>> list = new ArrayList<>();
        map.put("data",list);
        return map;
    }

    @PostMapping("/uploads") @CrossOrigin
    public Map<String, Object> upfile(HttpServletRequest req, MultipartFile file) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", req.getMethod());
        map.put("filename",file.getOriginalFilename());
        return map;
    }


    @PatchMapping("/save")
    public Map<String, Object> post(@RequestBody Map<String,Object> user, @RequestHeader String token, HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", req.getMethod());
        map.put("token",token);
        map.put("user",user);
        return map;
    }

    @PatchMapping("/add")
    public Map<String, Object> add(UserVo user, @RequestHeader String token,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", req.getMethod());
        map.put("token",token);
        map.put("user",user);
        return map;
    }

    @PatchMapping("/add2")
    public Map<String, Object> add2(int id,String name, @RequestHeader String token,HttpServletRequest req) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", req.getMethod());
        map.put("token",token);
        map.put("name",name);
        map.put("id",id);
        return map;
    }

    @GetMapping("/getusers")
    public List<User> getUsers(@RequestHeader String token) {
        System.out.println(token);
        List<User> us = new ArrayList<>();
        us.add(new User(10, "李四", LocalDate.of(1985, 12, 3)));
        us.add(new User(22, "张三丰", LocalDate.of(2006, 10, 5)));
        us.add(new User(33, "周强", LocalDate.of(2010, 6, 18)));
        return us;
    }


    @DeleteMapping("/del/{id}.html")
    public Map<String, Object> del(@PathVariable int id,@RequestHeader String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除ID为的" + id + "记录成功");
        map.put("token",token);
        return map;
    }

    @DeleteMapping("/dels")
    public Map<String, Object> dels(int id,@RequestHeader String token) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "删除ID为的" + id + "记录成功");
        map.put("token",token);
        return map;
    }


    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
